<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="queryData.query"
            @clear="getGoods"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
          >
            <el-button @click="getGoods" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goods" style="width: 100%" stripe border>
        <el-table-column type="index" label="*"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" label="重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time | formatData}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除 -->
            <el-button
              @click="removeGood(scope.row.goods_id)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[2, 4, 6, 10]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询参数
      queryData: {
        query: '',
        // 页码
        pagenum: 1,
        // 每页条数
        pagesize: 10
      },
      // 商品列表数据
      goods: [],
      // 总共商品条数
      total: 0
    }
  },

  created () {
    this.getGoods()
  },

  methods: {
    // 获取商品列表数据
    async getGoods () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goods = res.data.goods
      this.total = res.data.total
    },
    // 每页条数发生变化
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoods()
    },
    // 页码发生变化
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoods()
    },
    // 点击删除
    removeGood (id) {
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点击确认
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getGoods()
      }).catch(() => {
        // 点击取消
        this.$message.info('已取消删除')
      })
    },
    // 点击添加商品
    goAddGoods () {
      this.$router.push('/goods/addGoods')
    }
  }
}
</script>

<style scoped lang='less'>
.el-table {
  margin-top: 14px;
}
</style>
